﻿using HDSmartCommunity.BUS;
using HDSmartCommunity.DAL;
using Quartz;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace HDSmartCommunity
{
    public class TimeJob : IJob
    {
        public Task Execute(IJobExecutionContext context)
        {
            return Task.Run(() =>
            {
                ReportDAL da = new ReportDAL();
                da.DeleteModeRecord();
                da.DeleteEquipment();
                da.DeleteIncarevents();
                da.DeleteOutcarevents();
                DeleteLog();
            });
        }

        public void DeleteFile()
        {
            string logPath = AppDomain.CurrentDomain.BaseDirectory + "/Log";
            if (!Directory.Exists(logPath))
            {
                return;
            }
            RunCommand("chmod 777 " + logPath);
            DirectoryInfo folder_base = new DirectoryInfo(logPath);
            FileInfo[] files_base = folder_base.GetFiles("*.txt");  //获取.txt文件
            foreach (var file_txt in files_base)
            {
                //获取文件创建时间
                DateTime fileCreateTime = file_txt.CreationTime;
                //获取当前时间
                DateTime now = DateTime.Now.Date.AddDays(-14);
                if (fileCreateTime < now)
                {
                    try
                    {
                        File.Delete(file_txt.FullName);
                    }
                    catch
                    {
                        continue;
                    }
                }
            }
        }

        public void DeleteLog()
        {
            try
            {
                JsonConfigHelper jsonConfigHelper = new JsonConfigHelper("appsettings.json");
                int day = Convert.ToInt32(jsonConfigHelper["LogSave"]) * -1;
                string logPath = AppDomain.CurrentDomain.BaseDirectory + "wwwroot/Log";
                string logPath_2 = AppDomain.CurrentDomain.BaseDirectory + "wwwroot/TestLog/Network";
                string logPath_3 = AppDomain.CurrentDomain.BaseDirectory + "wwwroot/TestLog/Serial";
                DeleteFile(day, logPath);
                DeleteFile(day, logPath_2);
                DeleteFile(day, logPath_3);
            }
            catch (Exception e)
            {
                LogHelper.Log(e.ToString());
            }
        }

        public void DeleteFile(int day, string logPath)
        {
            try
            {
                if (!Directory.Exists(logPath))
                {
                    return;
                }
                DirectoryInfo folder_base = new DirectoryInfo(logPath);
                FileInfo[] files_base = folder_base.GetFiles("*.txt");  //获取.txt文件
                foreach (var file_txt in files_base)
                {
                    //获取文件创建时间
                    DateTime fileCreateTime = file_txt.CreationTime;
                    //获取当前时间
                    DateTime now = DateTime.Now.Date.AddDays(day);
                    if (fileCreateTime < now)
                    {
                        try
                        {
                            File.Delete(file_txt.FullName);
                            LogHelper.Log("删除日志" + file_txt.FullName);
                        }
                        catch
                        {
                            continue;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                LogHelper.Log(e.ToString());
            }
        }

        public static void RunCommand(string command)
        {
            Process.Start("sudo", command);
        }
    }
}
